package org.skyui.views;

import org.skyui.beanmanagers.BeanSetManager;
import org.skyui.beans.AbstractBean;

import com.vaadin.Application;
import com.vaadin.ui.Window;

public class BeanCreateView extends AbstractBeanView {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5440392831364379902L;
	protected AbstractBeansTableView parentBeansTable;
	protected BeanSetManager manager;

	public BeanCreateView(String name, String explanation, AbstractBean bean,
			Application application, ExistingBeansTableView parentBeansTable,
			BeanSetManager manager) {
		super(name, explanation, bean, application);
		this.parentBeansTable = parentBeansTable;
		this.manager = manager;
	}

	protected void actionAfterValidation() {

		Object newBean = this.manager.createBeanFromTemplate(this.item
				.getBean());
		if (this.parentBeansTable != null) {
			this.parentBeansTable.refresh();
		}
		removeBeanCreateWindow();
	}

	protected void actionAfterCancel() {
		removeBeanCreateWindow();
	}

	private void removeBeanCreateWindow() {
		Window subWindow = getWindow();
		Window appWindow = (Window) subWindow.getParent();
		appWindow.removeWindow(subWindow);
	}

}
